package com.pkk.penguin.frame.know.java.practice.stack.impl.listdocker;


import java.util.EmptyStackException;
import java.util.List;
import com.pkk.penguin.frame.know.java.practice.stack.impl.StackInter;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @description: 栈的list容器简单实现
 * @author: peikunkun
 * @create: 2020-01-03 16:37
 **/
public class StackDockerList<T> implements StackInter<T> {

  /**
   * list容器
   */
  private List<T> list;

  public StackDockerList() {
    list = new CopyOnWriteArrayList<>();
  }

  /**
   * @Description: 压栈
   * @Param: element
   * @Author: peikunkun
   * @Date: 2020/1/3 0003 下午 4:28
   */
  @Override
  public boolean push(T element) {
    return list.add(element);
  }

  /**
   * @Description: 弹栈
   * @return: T
   * @Author: peikunkun
   * @Date: 2020/1/3 0003 下午 4:29
   */
  @Override
  public T pop() {
    if (list.isEmpty() == true) {
      throw new EmptyStackException();
    }
    return list.remove(list.size() - 1);
  }

  /**
   * @Description: 栈顶端
   * @return: T
   * @Author: peikunkun
   * @Date: 2020/1/3 0003 下午 4:29
   */
  @Override
  public T peek() {
    if (list.isEmpty() == true) {
      throw new EmptyStackException();
    }
    return list.get(list.size() - 1);
  }

  /**
   * @Description: 栈指针是否为空
   * @Param:
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2020/1/3 0003 下午 4:29
   */
  @Override
  public boolean isEmpty() {
    return list.size() == 0;
  }
}
